#ifndef RAWOVERLAYBASE_H
#define RAWOVERLAYBASE_H


#include <QPicture>
#include <QPainter>
#include "math.h"
#include <QtGui>
#include <QtCore>


#include "Image_Config.h"






#include <iostream>
#include <stdio.h>
#include <math.h>


extern "C" {
#include "tiffio.h"
}
#include <QMessageBox>
#include <QFileInfo>
#include <iostream>
#include <stdio.h>
#include <QFile>
#include <math.h>
#include <QColor>
#include <iostream>
#include <stdio.h>
#include <QString>
#include <QImage>
#include <QVariant>
#include <QRegExp>
#define Q_UINT32 quint32
#define latin1   toLatin1


typedef uint UINT;
typedef uchar BYTE;
typedef qint64 ilong;
typedef qint64 ILONG;
/////typedef unsigned short WORD;
/////typedef unsigned long  DWORD;


class RawOverlay
{
public:
    RawOverlay( const qreal _mega , const QString file ) : m_mega(_mega),ti(file),d_loaded(false),ctype(NotSupported) { }
    enum ColorType {BilevelMinIsWhite, BilevelMinIsBlack, Gray8MinIsBlack, Gray8MinIsWhite, RGB24, RGB32, NotSupported };
    inline qreal CacheSize() {
        return m_mega;
    }
    inline void setCache( const qreal _mega ) {
        m_mega = _mega;
    }
    inline QString getFilename() {
        return  ti.absoluteFilePath();
    }
    void AfxMessageBox( QString  msg , const int mcase = 0) {
        if (mcase == 0)
            QMessageBox::warning(0, QString("Overlay Image Handler"), QString(msg));
        else
            QMessageBox::information(0, QString("Overlay Image Handler"), QString(msg));
    }
    int getBytesPerScanline( int width, int bpp, int paddedTo = 32 )
    {
        int temp = width * bpp;
        if ( temp % paddedTo != 0 )
        {
            temp += paddedTo - temp % paddedTo;
        }
        return ( temp >> 3 );
    }
    ColorType ctype;
    bool d_loaded;
private:
    qreal m_mega;
    QFileInfo ti;
};



const QVector<QRgb>& colorTable(RawOverlay::ColorType color);
qreal weightMega( const qint64 peso  );



class ImageMatrix
{
public:
    ImageMatrix( QSize original );
    ImageMatrix::~ImageMatrix();
    void rescale( const QRect rect );
    void request( const QSize viewer , const  QPoint point = QPoint(0,0) );
    void print();
    inline QRect irect()const {
        return QRect(20,20,csize(),rsize());
    }
    inline qint64 ID() {
        return id;
    }
    inline uint rsize()const {
        return rowsize;
    }
    inline uint csize()const {
        return coolsize;
    }
    inline bool work() {
        return godown;
    }
    inline int* arraycool() {
        return flatColumns;
    }
    inline int* arrayrow() {
        return flatRows;
    }
private:
    qint64 id;   /*  id nummer to save on cache */
    bool godown;
    QMatrix matrix;  /*  resize matrix */
    QMatrix imatrix;  /*  inverted matrix */
    qreal proportion;
    qreal roxmatrix;
    int* flatColumns;
    int* flatRows;
    uint coolsize;
    uint rowsize;
    QSize osize;
    QSize smallsize;
};




#endif